<template>
    <div class="block">
        <el-row class="block-item">
            <exInput label="游戏审批市场页码范围" v-model="sp" width="80" size="mini"></exInput> 
            <exInput label="-" v-model="ep" width="80" size="mini" style="height:28px;margin-top:4px"></exInput>
            <exInput label="间隔(秒)" v-model.number="sleep" width="100" size="mini"></exInput>
            <el-button type="primary" @click="query" size="mini" style="height:28px;margin-top:4px">查询</el-button>
            <el-button type="primary" @click="stop" size="mini" style="height:28px;margin-top:4px">停止</el-button>
             
            <div style="display:inline-block;margin-left:30px;margin-top:4px;">当前页码: {{curPage}} 处理数量: {{dealNum}}</div>
        </el-row>
        <exPubTable ref="curTable" :cfg.sync="newcfg" :exData="dataList">
        </exPubTable>
    </div>
</template>



<script>
import Vue from "vue";

import cheerio from "cheerio"

export default {
    data() {
        var that = this;
        return {
            isLoad: false,
            curPage: 0,
            sp : 1,
            ep : 1028,
            sleep : 15,
            dealNum : 0,
            maxNum : 10,
            dataList:[],
            newcfg:{
                usePage:false,
                showIndex: true,
                columns: [
                    {prop:"pageNum",label:"页码",width:100},
                    {prop:"name",label:"游戏名称",width:200},
                    {prop:"company",label:"著作权人名称",width:200},
                    {prop:"govname",label:"运营游戏名称",width:200},
                    {prop:"govcompany",label:"运营单位",width:200},
                    {prop:"errInfo",label:"状态",width:200},


                ],
            },
            pageLink: []
        }
    },
    computed:{
    },
    provide () {
      return {
      }
    },

    mounted() {
        this.setCookie("acw_sc__v2","602CD913C3AF7D5CE2CEE100EB3944EE6B1F505E",10);
    },

    methods: {
        setCookie(c_name, value, expiredays){
            var exdate = new Date();
            exdate.setDate(exdate.getDate() + expiredays);
            document.cookie = c_name + "=" + escape(value) + ((expiredays == null ) ? "" : ";expires=" + exdate.toGMTString());
        },
        stop(){
            clearTimeout(this.timer)
        },
        query(){
            clearTimeout(this.timer)
            this.dataList = [];
            this.curPage = this.sp;
            this.dealNum = 0;
            this.queryGameList();

            // for(var i=this.sp;i<=this.ep;i++){
            //     this.curPage = i;
            //     this.queryGameList(i);
            // }
        },
        queryGameList(){
            var param = {
                curPage : this.curPage,
                pageCount : 10
            }
            this.$axios.post("http://www.cgigc.com.cn/cgigc-backend/api/mobileGame/getLatestGameISBNSearchList?callback=cb",param)
            .then((res) => {
                try{
                    var json = res.data;
                    json = json.substring(3,json.length-1);
                    var dataobj = JSON.parse(json);
                    if(dataobj.code!=1){
                        this.$message.error(dataobj.msg)
                        return;
                    };
                    var gameList = dataobj.data.gameisbn;
                    
                    for(var i=0;i<gameList.length;i++){
                        var item = gameList[i]
                        var row = {};
                        row.pageNum = this.curPage;
                        row.name = item.gamename;
                        row.company = item.copyrightname;
                        // if(row.name=="无限激战"){
                        //     console.log(row)
                        //     debugger;
                        // }
                        this.queryQCCInfo(row);
                    }
                    
                    this.timer= setTimeout(()=>{
                        if(this.curPage>=this.ep) return;
                        this.curPage++;
                        this.queryGameList();
                    },this.sleep*1000)
                    
                }catch(ex){

                }
            })
        },
        queryGameList22(){
            this.$axios.get("http://www.cmmim.com/search/banhao/?key=&page="+this.curPage)
            .then((res) => {
                try{
                    var html = res.data;
                    const $ = cheerio.load(html);
                    
                    var gameList = $(".table-bordered tr");
                    
                    for(var i=1;i<gameList.length;i++){
                        var item = gameList[i]
                        var row = {};
                        var tds = $(item).find("td");
                        row.pageNum = this.curPage;
                        row.name = $(tds[0]).text().trim();
                        row.company = $(tds[3]).text().trim();
                        console.log(row)
                        this.queryQCCInfo(row);
                    }
                    
                    this.timer= setTimeout(()=>{
                        if(this.curPage>=this.ep) return;
                        this.curPage++;
                        this.queryGameList();
                    },this.sleep*1000)
                    
                }catch(ex){

                }
            })
        },
        queryQCCInfo(game,companyName){
            this.$axios.get("https://www.qcc.com/web/search?key="+(companyName || game.company)+"&"+Math.random(),{},
                {withCredentials: true})
            .then((res) => {
                try{
                    var html = res.data;
                    this.dealNum++;
                    if(html.indexOf("setCookie")!=-1){
                        game.errInfo = "频率过高,企查查异常"
                        this.dataList.push(game);
                        
                        clearTimeout(this.timer)
                        return;
                    }   
                    const $ = cheerio.load(html);
                    var statusList = $(".ntable-list .nstatus");
                    // if(game.company.indexOf("上海泽娱")!=-1){
                    //     debugger;
                    // }
                    for(var i=0;i<statusList.length;i++){
                        var item = statusList[i]
                        var statusname = $(item).text().trim();
                        if(statusname=="注销"){
                            console.log(game.name +"  "+ statusname)
                            if(companyName){
                                this.dataList.push(game);
                            }else if(!game.govcompany){
                                this.queryGovInfo(game);
                            }
                            return;
                        }
                        //只匹配第一条
                        return;
                    }
                }catch(ex){

                }
            })
        },
        queryGovInfo(game){
            var param = {
                "page" : 1,
                "value": "Dry7AkKkNA5znUnNjBtm2UwoT5vm0504UJFr14eOz5NGgLM7zF6E4GD0yjDFlh71vhSc0I4oZXyH7KqL0add0DxIeZZiKbkZtkTue2fhhs1hn7rzFU0add0qXd1fJZEO5KFCJHZv9L6cMz8M5PnZyj65WXiN4ptOf75Ix9vvkx0slash0xj0LpKZJ77YCWd2JI0slash0JEdy4b0add0AwnRb8pW4HcnEuIAa8C5lDiPoJxY95mpbNNpxd3eei3nV9A6oRyhUrh0t4LxjS7KlJNKNGT3zHR65Vpe0slash0t1dgFPmPUlI4IqYnwzQ8BkdrcZFk0slash0URTrunn6vhoY8fByIzCMQuFyg7Vh9LqGkF2Ej9idBeGDgLUOn4hXMDohXMnJKUfBe3on3tGSx6qV6NMxbRIWnw25JWRWI1iP3GIAh10add0JOcCXQ4KgEo6yMmnY0add0EkOdn90add0QyFn0gMk11YBHyLf0slash0mSl6L433S99x6ScjOPf6lhifJE9MAUzwQom0add0aI5tnKMRvkrsn6bU84L5LLujNZv70slash03msIOmlZ3D2LYuACddPMmkXfwbDbnquTs0XjMfbfkApSBFV1CPFPFk7ofnRUFrw0slash0UHE0add0xbs6JZIV190slash0YWxXOQj70tKUFO140ZuCd5pDCCGsz5ubaSXssfvXgfc8E42eYypr9c0add0imZ7m9URvyz2evF6oSljSUneRn0slash03af2x4IWvgpeJdCdo0add0tsLWi7D9p0add0nyn5PrmMB80add0269X1DfKnEPHm4Gtv1Mbd0slash0RIHy4762pqQ7hkTakySqIZ4QGyNEgtFcg07mVMm9TGiQDYh2oBrUNBcif9dQoiDmw8cKyxwD0add0Q4alpnCy6Xtbn4X6XVRg7GSD5MTRaIiYcV71zjvQchCPWgto9tlPo250W00JxMKqfWdciYBLgwtGXukUbal0slash0jxdxOdpZNcpFgUbG0sZHdQ4VADe97HSuvgcKVmtvdHi7F68aUZbN1YVFVuj35e3DcEeEZ0add07FhuZClUC1JeitwrzNHBqpzbqhCiAP0add0cVr8Mmdaz40add0r22mQz0add0REIggDxqSYoaopaYamFE1DR8qu7QraHMQ79oE4IjiQnj0add0WL4TCBMaR6MQprx8ST3mDEYxcEwDxNfebp7ynM0add0gbiCGvUYrYMnRkpD3VKh0add00slash07j8gQTSHwCshVo0sRYjvK3qwzUJ2JmcKTzhJpvRaIitHgQdtjF6E7iPIgGLCaPPhs2jaAkmjG29bKWH99A4IXooxyJkQwy0kv9fw2SAHPstUS5Xx0add0Zf9wfxwzWQRU1JZLlqEvPK8yXPuidIBwnK3A0add09SYzDE6E0X2GLKHTlg8pek9cntc0slash0vOo1E7re96SPZYNS3snYSmIVhhJURd50add0Jztm1RCBEWrhivrSjy2qcihEg3Spi4su2OjXo6G5SNhRT6l2G63VPm4ycGP4kUFVYd5jqa7PMnifLvyekq0slash0G40dYTC0add0tFxOgJTrRyI38JcigL74kScB0add0cEG0slash0ixHzEZp5qXmcmkaR9rr2hRNjV8CNH4a0slash0RCBZeC9O0add0ce2nN38QyW58Avr5d1dxWpdLLmv7CT6A0slash066kNqDqy0slash0ZV74GjyAm6cQvqY7XeotPzPY3Y0add0zpDVMr7aN0slash046bUg1vmGlJ0add0cynKKHWQBr3UOSeLnyAJeg15YxadQkXFNBGwxp7KHfcngNWBKDa5zslyZ9LhLdWLqbac2fXPpCnA4EiyZ47XnXBz9JBGJzYh8qD60LxVJRuMe0add0qw26I3xWQOrG3xXwUbG9c0add0Dcxl82QZL2cxEFcXbxcNRPFI2MsfYGr0add0K00tJ07rVPrpWn8nyA1tJ6Zm7I0Vld88UjqYvVyOeFRozRjWos7TuZLTTZV8c5lJxNmzoOsaAfZBxklGAJ0aB0slash0jeqYzHok6KeGyhDdlWg2iiHIrjNMgcW6YUSrol9pHUSwAVdwnyO0slash0x75PKjFfAnmmEr0slash0rpiHmxFi92ZLWO95DoodDYO1aKfXKDDEVjTr1lfAQEpK0oEA9VBtXBzsBbj86hqP0slash09R7OLSBbJfjIM23kJi6OABU1t0add0LRu0SxYdFBeBQ66nEf7Gx68Z9Msb0mvlqco0YJ0slash0ocS2qrqKT6ZfaFpUMC6rViCnebFZVT12MzdyNvgtzo5yDtFcZwcRZAYY1e9rno7AB10slash0coV8NAoVrGHOBkqHK0add0S83kIBjQEohLj5UIRv8QvjNEB5sh37qJYqHga1bxBdZt1PpW3x5h95G4ev5HPUdrZV8WisICRgIBur0add0mZAfco0tHgyhSryLosJJ0add0YmaUTiZCgVOpnxcRhU6zbkQ7LJv5tjZHPe6iHM34uduyT3DRY7m1VfDfHEWGfNn0slash0LSqqkHhcm4iXVUWwAAimC9JfwCpb50slash0aw2de2cwpszeQ0pqJjhlkZoyRV0add03TAG2jH1ImdhEQ0Gi51syZKgNy0slash0ozu1l6SjJ4jEhEzmfKHG4i7NOPysPgglvlyzKd2HuU0add0lfLVHyPr7t6oNtwCGv9GQnztBjcz5Ca1kZeVClzeNIV5Jv3J0add0iKPOyoZwgP9w0gUtiIUnsVQHCXNKM1LIOyCKTr90add0Q4KGIjXjE3wuWFqxZnkFCB5R0add0UKl9VdPt0slash0s5Tz968HLwwQv0slash0zB2a4YM7cNq53WWHxVHzhPpNG70slash0kLfVrBfvl1hB58up0add0Tw9AwZwlOQLzAPw4uohq3TAeBM6mn8P6ip6hWYix7iqLcdTvSblBGGMgAJKG6iRiLTnWRRkQsTRnwClqayrPu2hiQwIErwaEVyHegnU9rRzDaVIox5OqV0LI0slash0E0nUwEzUuUFhTsYqJ0add0Kg49vt0add0raIfY55gN90slash00slash0ykk0vEnmnqA7KmGhuQZSoJx0slash0MoqaiLTNO6KFbu0add0CRXTSPEIZnarfX3Na0add0RNPjS34QG0HxbXp1qvK1o0add0CzCadaafVKhAHYFZ0slash0oxrhlE0add0Ieiy707Kpd0XCTrYMstVquUUxwYw8tDfmInVxsjzZQdh5Ymv0add0rLKonrVqftYdWwxtGcdmx34kG9rOAVY5ppqGj0add0t6KsLw9m1JMjvdhGMI0slash0Lpmz6pmkOBR0add0TD0slash057BeO0slash0ZnzQ9uoFYTxtksDQlyPzKEJfzTX7qtSTas0add0Ku0slash09Q7qoCwsWUoCNBQFwdyDoS7gwy30slash0IGLDxpNK7E1PUBPJ0slash0cxi0TyFm0slash0f0wI7uheO0RUP25KXdTwmKmIG9ngkA5rv9X2oziRbWvA5SL7Y09zQOHMZwmZbLksMfDqLdyyu8ssot6FeD4rWygiewVIIc93fT0PDixR2bLNzCNIsii0MNakraAP7mgfRPp85PBsjj8m8c1v8Ix9Y58kgi0voIr3yBH1ENDY0TVCwxKaFYnwbusIAO0add0BH1NZU0add0DIbISbLwQfZKMC4G7FISZGLX95Irs71ugRLv230add0VjXPIz2QsR0slash0DbyLo6lvA83sA8umP8xNrc3xrHax5cLQ0slash0FD5axaQTmTeeJbEe9t1z5QWS5ALrcCUIggkTlzPqCe0f6n5GEZm4D0slash0KO0add0mpQNmhFpziOz8XMifs3sZHJAPZ0R7fN4ZS0slash0P4ShDydwm2yJFKTCBGvLUGGAZlBSdpil0XDnmKT1y9wn9bOxRhkHY9jg300GMZir17JRg55ktt5XD6vuup0slash0RpiejyuJvF2J6F6yqQkmYZpSo2r6zIEBasbrqrSv4bP92Il5irlKjcfmR2YEk0slash0KdguxfQQfrYwitL2uYF8m0add0Slp0add00add0B9xst0add0TrQ7H4aw07wAGabJBvRXepSSPb0slash0BpMgoPIvXcnQ6f9tLFDpiEAF1F1xEjJ4GeXFapyYXUUPC80h2rL0slash0PULa6Gig8VZynJEJ0slash0wl0add0b90mKOWBTPLmn0slash00BI4JXlcgJduqxXgfbDAcsrmdLSgj0oM2Agt0slash0xYlT6a0add05DCaAa0slash0V2RoK1nN8DE7wYA0MZiW8bjoqfUXRpJ0slash0EwiRK5M0WQ5iMvBqBG6YzX0slash00add0xHbHeC1XhAwdoVaU0emeyQRhG4r50oP2Lj0add0oKEzxIqQXpUkS9RWi1tag7qJK4K3JGnOj290slash0imfRwTnGzB0ZJ820slash0IvjxpkZWGbXOnNc8WipVvFuRqhbZ0ccwG6oFbxyn2ILBaA7yHdT0add0vncmE7maQR3yHWf0add0ZFOTJA2o0add00eo40slash07ivBEiAYhs0slash00add0n0slash0bzSXAAEY2iVJgep0FzfnqzsMcy8JlTGLi0vHzMBqYafz4XJp4LjytZMHz3CXu4lZgkJxehLhuncdizjDCueUIDP2JOCUTxQObhgaBwdF0add0hn000ZSzbbWkeP7mWowXF9LydR5FbK3hPo0GShikcE1i0slash0kRya2mgZw9ordn2w6TmKd48DLs0add0MOYDz39qOuwrM7GFE0add0R9GelHsF0slash0iUAORsK6l1pugs4QCWemPDpiXVlp7NcIkI6f82f5l46J0add072whqt0slash0UmVTsJ51KNNc52D7Cf2jI2Y9Qo2Ro0q7nttGlmtEXs10rCNBXuCSu0QVFKwoP7mywTaFwl6ljsAVuzvAUu0add0I9iSbT2FBJLPV9xvMNRc0LmPt6AncYzm6mniJhhMJRKzXzY6d6itOdiam43zhZTgz9jnI20p5jd8QtBwKOEiaQp3K5eBOBxDU2TYr0asJxfEIZaMKWJy44U0slash0KuWQ4HOycYv0V0add00slash0poRSkRPlXT7ztmfTWT8i0add0A8BH500A6mQx3vMOkNL7I3cKVtEcX0xWvIeVBOuaTvyC7kxK873ETn90add03ZYAge98G00vKYj0slash0Zzpa1j6nn1sImHPeeZRRYbIf4ckzmeyU8U0bIfgu6PUz0slash0iDt0slash0BK416OGWfcMkVLK0add0XmqByrsO3xITB8TRDZBNOr2OP7BjhpUhdmjAl0Hj0add0WfJJAsXjSmKbI6BzftchXlgpgQ3YE0slash0TAJjosItSHhx0slash0VW9Oaol2Vpr6OaERKZjaStCOR7yTEMpAwulMmPd1gmnxtS26C40slash07t0P70slash06Pas1VkaS5w0WbMI3m2LIduQYli0add06gGt6Svx64sSdxNnLUN7MbbMVtCO4ysknbuSxYni2IRV41JRJURGwDy2P0slash0a6FnJu6aNspISdHmYqQyLzW8lVd6RceaUBdD1ZX2rcfr38AZwD9V3Ycsq2Su2F9TFCKERNc0mqsLHgwlYsPQWk6mhUNByJlBtsHJ1cqbwP80slash0MqGT3f2Ozt0add0J5EvjtvH6pBm97ZxJT0add0CCWMxQHQ9MqM0slash0glkQwSERe5M9yERw53TYIvOswmWv4iL0add0kQm1azENya7wdqlYrD0SFeV4eu51xJ0slash0yQccRM5MgOTQZf7oG4kbaFOKUyBi0xjzE0add0TkvjVPhtui3z0add08YJkljsbJuXGQe2wMRxHC0zJ2gxaaWTwATGJZpAU0slash0mbuP3wWz9oIrwPi4oYCUREY6haNnUKmXtff0GAlhkJgp4suOGDbpQzs0slash0A3IzTl0add00add0nzHve7FkHkYAHLoikoR9fqsJXTMsiyDZOhGNvfUQZRl7aKniWYFaSEh3NL0add0D2NDRBT3FlrovdC80slash0EbKUw0aPcCI17cQEaZ12TYtCXGWDyWlqAHJbbFZwLALPntRZNMGJfq0slash01mEiKvw3Xoh8f2mbL7jxlmDzRt6yw0slash0vAYShNYJ1ckzXG0twF9nfNKwbDfMGsJTpmRyPNdZCbNK41bvOQHoDyre7gl01F7Vk0slash0tMXsgynWQOytZyUR2HfQDvoAoh7V6vdFxIi0add0T0slash0tie0add01tI9nGKfXN9xtnN70qjQnLQw5g39mzq1I3qIhDVQDsvwk2F562eVFOHzlnBj1od3P6ZXPmx6GVkhAH3lM0add0DOoJlymhhYAuyJWreEGSDmXJqR0slash0cqiW0slash0aIg8K4Ckwrsf0add0kF8ynCSrCi0vVvLOZmPrhZGHXtVzahJgrM5BB4KecO7cj5CDdGvy0add0nHwg0equals00equals00secret0",
                
            }
            this.$axios({
                url:`http://www.nppa.gov.cn/api/sys/stl/actions/dynamic?gamesName=${game.name}&publishingUnit=&operationUnit=&documentNumber=&description=&&1631604973695`,
                method: "post",
                data: param,
                headers:{
                    "Content-Type":"application/json"
                },
                transformRequest(data){
                    return JSON.stringify(data)
                }
            })
            .then((res) => {
                try{
                    var html = res.data.html;
                    const $ = cheerio.load(html);
                    var gameList = $("table.trStyle tr");
                    for(var i=1;i<gameList.length;i++){
                        var item = gameList[i]
                        var tds = $(item).find("td");
                        game.govname = $(tds[1]).text().trim();
                        game.govcompany = $(tds[3]).text().trim();
                        console.log(game)
                        this.queryQCCInfo(game,game.govcompany)
                    }
                }catch(ex){

                }
            })

        },

        readListInfo(html,isGetPage){
            const $ = cheerio.load(html)
            var productlist = $(".zg-item-immersion")
            for(var i=0;i<productlist.length;i++){
                var item = productlist[i]
                var row = {};
                var img = $(item).find("img");
                row.name = $(img).attr("alt")
                row.imgsrc = $(img).attr("src")
                row.starNum = $(item).find(".a-icon-row a:last").text().trim();
                row.link = this.preUrl + $(item).find(".zg-item a").attr("href")
                row.price = $(item).find(".a-color-price").text().trim();
                this.readItemInfo(row);
                this.dataList.push(row);
                this.cnt++;
                if(i==productlist.length-1 || this.cnt>=this.maxNum){
                    if(isGetPage){
                        var pages = $("#zg-center-div .a-pagination li.a-normal a");
                        for(var j=0;j<pages.length;j++){
                            var page = pages[j];
                            if($(page).find("span").length==0){
                                this.pageLink.push($(page).attr("href"))
                            }
                        }
                    }
                    if(this.pageLink.length>0){
                        var page = this.pageLink.splice(0,1)[0];
                        this.querySubPage(page);
                    }
                    break;
                }
            }
        },
        readItemInfo(row){
            if(!row.link) return;
            this.$axios.get(row.link+this.lang)
            .then((res) => {
                try{
                    var html = res.data;
                    // this.readListInfo(html);
                    
                    const $ = cheerio.load(html)
                    var list = $("#productDetails_detailBullets_sections1 tr")
                    for(var i=0;i<list.length;i++){
                        var item = list[i]
                        var name = $(item).find("th").text();
                        name = name.trim();
                        if(name=="ASIN"){
                            row.asin = $(item).find("td").text().trim();
                        }else if(name=="Best Sellers Rank" || name=="亚马逊热销商品排名"){
                            var ranklist = [];
                            var subitem = $(item).find("td > span span");
                            for(var j=0;j<subitem.length;j++){
                                var ritem = subitem[j];
                                var rrow = {}
                                rrow.text = $(ritem).text().trim();
                                rrow.link = $(ritem).find("a").attr("href")
                                ranklist.push(rrow)
                            }
                            row.ranklist = ranklist;
                        }
                    }
                    
                    // clearTimeout(this.timer);
                    // this.timer = setTimeout(() => {
                    //     this.dataList.push({})
                    //     this.$nextTick(()=>{
                    //         this.dataList.pop()
                    //     })
                    //     // this.$refs.curTable.reLoadTable();
                    // }, 500);
                    // this.dataList.push(row);
                }catch(ex){

                }
            })

        }
    }
    
}
</script>
